package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyCollect;
import cn.wolfcode.trip.base.mapper.DailyCollectMapper;
import cn.wolfcode.trip.base.mapper.DailyMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class DailyServiceImpl implements IDailyService {
    @Autowired
    private DailyMapper dailyMapper;

    @Autowired
    private UserMapper userMapper;



    public void saveOrUpdate(Daily entity) {
        if (entity.getId() == null) {
            dailyMapper.insert(entity);
        } else {
            dailyMapper.updateByPrimaryKey(entity);
        }
    }

    public PageInfo<Daily> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Daily> list = dailyMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public List<Daily> ListCommends(Integer state) {
        return dailyMapper.selectByState(state);
    }

    @Override
    public Daily get(Long id) {
        return dailyMapper.selectByPrimaryKey(id);
    }

    @Override
    public String collect(Long dailyId, Long userId) {
        //设置当前页面返回值
        String msg;

        //查询当前用户和当前日报在数据库是否有数据
        Map<String, Object> map = userMapper.selectCollect(dailyId, userId);
        //当有数据时删除当前数据并设置返回值为取消
        if (map!=null){
            //有数据进行删除数据
            userMapper.deleteUser(dailyId,userId);
            msg="取消收藏";
        }else{
            //没数据时进行新增数据
            int state=1;
            userMapper.insertUser(dailyId,userId,state);
            msg="收藏成功";
        }

        return msg;
    }


    public List<Daily> listEnable() {
        return dailyMapper.selectEnable();
    }


}
